package com.vm.osm.business.controller;

import com.vm.osm.business.common.ControllerExceptionHandler;
import com.vm.osm.business.dto.req.orderInst.ReqAcceptByDept;
import com.vm.osm.business.dto.req.orderInst.ReqAssignOrder;
import com.vm.osm.business.dto.req.orderInst.ReqBackOrder;
import com.vm.osm.business.dto.req.orderInst.ReqRefuseByDept;
import com.vm.osm.business.service.ICheckPlanInstService;
import com.vm.osm.business.util.UserDataUtil;
import com.vma.eams.spring.boot.annotation.LogFunc;
import com.vma.eams.spring.boot.annotation.LogModule;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by chenzui on 2018/4/14.
 */
@RestController
@LogModule(type = "order",name = "工单管理")
@RequestMapping("/v1.0")
@Api(value = "巡检任务API",description = "巡检任务API",tags = {"AppTaskController"})
public class AppTaskController extends ControllerExceptionHandler {
    //================================================故障工单操作=======================================================
    //==============接单，拒绝接单，(外派的)抢单，（外派的）指派，（外派的）受理，（外派的）拒绝受理，转派给外部服务商，处理完成，确认，退回，撤销，评价
    private Logger LOG = Logger.getLogger(this.getClass());

    @Autowired
    private ICheckPlanInstService checkPlanInstService;
    @LogFunc(type = "accept",name = "接单")
    @ApiOperation(value = "巡检任务接单",notes = "巡检任务接单")
    @RequestMapping(value = "/app/task/accept",method = RequestMethod.PUT)
    public void agree(HttpServletRequest request,
                      @ApiParam(value = "order_id",name="order_id",required = true) @RequestParam("order_id") String orderId){
        LOG.info("巡检任务接单");
        checkPlanInstService.agree(orderId, UserDataUtil.getStaffId(request),UserDataUtil.getStaffName(request),UserDataUtil.getStaffHeadImg(request));
    }
    @LogFunc(type = "refuse",name = "拒绝工单")
    @ApiOperation(value = "巡检任务拒绝接单",notes = "巡检任务拒绝接单")
    @RequestMapping(value = "/app/task/refuse",method = RequestMethod.PUT)
    public void disAgree(@ApiParam(value = "order_id",name="order_id",required = true) @RequestParam("order_id") String orderId,
                         @ApiParam(value = "type",name="type",required = true) @RequestParam("type") String type,
                         @ApiParam(value = "reason",name="reason",required = true) @RequestParam("reason") String reason,
                         HttpServletRequest request){
        LOG.info("巡检任务拒绝接单");
        checkPlanInstService.disagree(orderId,type,reason,UserDataUtil.getStaffId(request),UserDataUtil.getStaffName(request),UserDataUtil.getStaffHeadImg(request));
    }

    @ApiOperation(value = "巡检任务抢单",notes = "巡检任务抢单")
    @RequestMapping(value = "/app/task/catch/{id}",method = RequestMethod.PUT)
    public void catchOrder(
            HttpServletRequest request,
            @ApiParam(value = "id",name = "id") @PathVariable(value = "id") String id){
        LOG.info("抢单");

        checkPlanInstService.catchOrder(id,request);
    }
    @LogFunc(type = "accept",name = "受理工单")
    @ApiOperation(value = "受理",notes = "外部派来的巡检单,需要受理操作")
    @RequestMapping(value = "/app/task/out/accept",method = RequestMethod.PUT)
    public void outAccept(@RequestBody ReqAcceptByDept reqAcceptByDept, HttpServletRequest request){
        LOG.info("受理");
        reqAcceptByDept.setOperate_staff_id(UserDataUtil.getStaffName(request));
        reqAcceptByDept.setStaff_icon(UserDataUtil.getStaffHeadImg(request));
        reqAcceptByDept.setOperate_staff_id(UserDataUtil.getStaffId(request));
        checkPlanInstService.outAccept(reqAcceptByDept,request);
    }

    @ApiOperation(value = "拒绝受理",notes = "外部派来的巡检单,需要拒绝受理操作")
    @RequestMapping(value = "/app/task/out/refuse",method = RequestMethod.PUT)
    public void outRefuse(@ApiParam(value = "order_id",name="order_id",required = true) @RequestParam("order_id") String orderId,
                          @ApiParam(value = "type",name="type",required = true) @RequestParam("type") String type,
                          @ApiParam(value = "reason",name="reason",required = true) @RequestParam("reason") String reason, HttpServletRequest request){
        LOG.info("拒绝受理");
        checkPlanInstService.outRefuse(orderId,type,reason,request);
    }
    @LogFunc(type = "back",name = "退回")
    @ApiOperation(value = "退回",notes = "处理的不行，退回重新处理")
    @RequestMapping(value = "/app/task/back/{id}",method = RequestMethod.PUT)
    public void again(HttpServletRequest request,
                      @ApiParam(value = "remark",name = "remark") @RequestParam(value = "remark",name = "remark")  String remark,
                      @ApiParam(value = "id",name = "id") @PathVariable(value = "id") String id){
        LOG.info("退回");
        checkPlanInstService.again(request,id,remark);
    }
    @LogFunc(type = "confirm",name = "确认工单处理")
    @ApiOperation(value = "确认",notes = "确认,订单完成，进入评价环节")
    @RequestMapping(value = "/app/task/confirm/{id}",method = RequestMethod.PUT)
    public void confirm(HttpServletRequest request,
                        @ApiParam(value = "id",name = "id") @PathVariable(value = "id") String id){
        LOG.info("确认");
        checkPlanInstService.confirm(request,id);
    }

    @LogFunc(type = "dispatch",name = "转派工单")
    @ApiOperation(value = "派单给员工",notes = "派单给员工")
    @ApiImplicitParam(value = "reqAssignOrder",name = "reqAssignOrder",dataType = "ReqAssignOrder",required = true)
    @RequestMapping(value = "/app/task/dispatch",method = RequestMethod.PUT)
    public void dealUpOrder(@RequestBody ReqAssignOrder reqAssignOrder, HttpServletRequest request){
        LOG.info("派单给员工");

        checkPlanInstService.dispatch(reqAssignOrder,request);
    }

}
